package com.zhongya.havefun.ui.activity

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Handler
import android.util.Log
import android.view.KeyEvent
import androidx.core.app.ActivityCompat
import androidx.lifecycle.lifecycleScope
import com.bytedance.sdk.openadsdk.AdSlot
import com.zhongya.havefun.R
import com.zhongya.havefun.app.BaseAppActivity
import com.zhongya.havefun.databinding.ActivityWelcomeBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import com.bytedance.sdk.openadsdk.TTAdSdk

import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTSplashAd
import com.zhongya.havefun.app.util.TTAdManagerHolder
import com.zhongya666.library.util.SPUtils


class WelcomeActivity : BaseAppActivity<ActivityWelcomeBinding>(R.layout.activity_welcome) {

    private var firstTime: Long = 0
    private var mTTAdNative: TTAdNative? = null

    override fun initView() {

        if (ActivityCompat.checkSelfPermission(
                this@WelcomeActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            ActivityCompat.requestPermissions(
                this@WelcomeActivity,
                arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                1
            )
        }

        toMain()

        //创建TTAdNative对象，createAdNative(Context context) context需要传入Activity对象
        //创建TTAdNative对象，createAdNative(Context context) context需要传入Activity对象
        mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
        loadAD()
    }

    private fun loadAD(){
        val adSlot = AdSlot.Builder()
            .setCodeId("887680902") //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告，请在穿山甲平台查看
            //view宽高等于图片的宽高
            .setExpressViewAcceptedSize(100f, 1000f) // 单位是dp
            .setImageAcceptedSize(100, 1000) // 单位是px
            .build()
        mTTAdNative?.loadSplashAd(adSlot,object : TTAdNative.SplashAdListener{
            override fun onError(p0: Int, p1: String?) {

                Log.e("wzy","$p0")
                Log.e("wzy","$p1")
            }

            override fun onTimeout() {
                Log.e("wzy","onTimeout")
            }

            override fun onSplashAdLoad(p0: TTSplashAd?) {
                mDatabind.rl.addView(p0?.splashView)
            }

        },3000)
    }

    private fun toMain() {
        /**
         * runBlocking 与 coroutineScope 的主要区别在于后者在等待所有子协程执行完毕时不会阻塞当前线程。
         */

        lifecycleScope.launchWhenCreated {
            delay(10000)
            val intent = Intent(this@WelcomeActivity, MainActivity::class.java)
            startActivity(intent)
            finish()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
            val secondTime = System.currentTimeMillis()
            if (secondTime - firstTime > 2000) {
                firstTime = secondTime
                return true
            } else {
                System.exit(0) //结束线程

            }
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun createObserver() {

    }
}